package com.mt.fpb.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mt.fpb.enums.ResponseCode;
import com.mt.fpb.mapper.*;

//import com.mt.fpb.model.PhoneMessage;
import com.mt.fpb.model.InsurancePolicy;
import com.mt.fpb.model.RegisterUser;
import com.mt.fpb.model.RxEmployee;
import com.mt.fpb.model.vo.CommonResult;
import com.mt.fpb.utils.HttpUtils;
//import com.mt.fpb.utils.TencentCloudAPITC3;
import com.mt.fpb.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("register")
public class RegisterController {

    @Autowired
    private RegisterMapper registerMapper;

    @Autowired
    private RxEmployeeMapper rxEmployeeMapper;

    @Autowired
    private InsurancePolicyMapper insurancePolicyMapper;

    @Autowired
    private RedisUtil redisUtil;
//注册接口http://localhost:9595/register/save?name=zhang&phone=15811011928
    @PostMapping("save")
    public CommonResult saveRegisterUser(String name,String idCard, String phone,String openId, String code) {
        log.info("获取到的name：" + name);
        log.info("获取到的phone：" + phone);
        log.info("获取到的code：" + code);
        log.info("获取到的身份证号:"+idCard);
        log.info(("前端传过来的openId:"+openId));

        String msCode = (String) redisUtil.get(phone);
        System.out.println("后台redis存储的msCode:"+msCode);
        log.info("验证手机短信验证码,有效期为5分钟");

        if (code != null && !"".equals(code) && msCode != null && !"".equals(msCode)) {
            if (msCode.equals(code)) {
                log.info("验证码验证成功，进行实名认证");
                //进入注册页面，先对要注册的用户进行一个实名认证，认证通过后才能注册即用户绑定
                QueryWrapper<InsurancePolicy> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("name_policyholder",name);//投保人姓名
                queryWrapper.eq("phone_policyholder",phone);//投保人手机号
                queryWrapper.eq("certificate_number_policyholder",idCard);//投保人证件号码
                List<InsurancePolicy> insurancePolicyList = insurancePolicyMapper.selectList(queryWrapper);
                //判读保单集合中是否有数据，有数据即能通过认证，反之不能通过认证
                if(insurancePolicyList.isEmpty()){
                    log.info("1保单数据为空，认证失败，进行第二步是否是白名单检测");
                    //2查询员工表是否存在白名单，白名单数据通过则进行更新openid
                   String flag = whiteListResult(name, idCard, phone, openId);
                       if("fail".equals(flag)) {
                       return CommonResult.fail(500,"用户绑定失败,认证不通过");
                   }else if ("success".equals(flag)) {
                       return CommonResult.success(ResponseCode.SUCCESS);
                   }
                }
                RxEmployee rxEmployee = new RxEmployee();
                rxEmployee.setName(name);
                rxEmployee.setPhoneNumber(phone);
                rxEmployee.setIdCard(idCard);
                rxEmployee.setOpenId(openId);
                rxEmployee.setRoleId(0);
                //查询员工表中是否有该条数据
                QueryWrapper<RxEmployee> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("open_id",openId);
                RxEmployee employee = rxEmployeeMapper.selectOne(queryWrapper2);
                if(employee==null){//没有数据进行插入操作
                    //判断是不是白名单，是白名单只进行更新openid操作
                    String flag = whiteListResult(name, idCard, phone, openId);
                    if ("success".equals(flag)) {
                        return CommonResult.success(ResponseCode.SUCCESS);
                    }
                    int result = rxEmployeeMapper.insert(rxEmployee);
                    if(result<1){
                        log.info("小金库用户插入失败");
                        return  CommonResult.fail(ResponseCode.FAILED);
                    }
                    log.info("绑定成功");
                    return CommonResult.success(ResponseCode.SUCCESS);
                }else{
                    return CommonResult.success(ResponseCode.SUCCESS);
                }
            } else {
                log.info("短信验证码验证失败");
                return CommonResult.fail(ResponseCode.FAILED);
            }
        }else {
            log.info("code获取失败");
            return CommonResult.fail(ResponseCode.FAILED);
        }

    }

    private String whiteListResult(String name,String idCard, String phone, String openId) {
        QueryWrapper<RxEmployee> rxQueryWrapper = new QueryWrapper<>();
        rxQueryWrapper.eq("name", name);
        rxQueryWrapper.eq("id_card", idCard);
        rxQueryWrapper.eq("phone_number", phone);
        rxQueryWrapper.eq("name_auth", 1);
        RxEmployee rxEmployee = rxEmployeeMapper.selectOne(rxQueryWrapper);
        if (rxEmployee == null) {
            return "fail";
        } else {
            log.info("1保单数据为空，认证失败，2是白名单，为此白名单客户更新openid");
            rxEmployee.setOpenId(openId);
            rxEmployeeMapper.update(rxEmployee,rxQueryWrapper);
            log.info("为白名单客户更新openid成功");
            return "success";
        }
    }
}
